//
//  ZKNavgationController.swift
//  Weibo
//
//  Created by 郑克哥哥 on 2017/7/22.
//  Copyright © 2017年 郑克哥哥. All rights reserved.
//

import UIKit

class ZKNavgationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //手势代理,设置pop返回手势
        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    //监听push操作
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //隐藏tabBar
        if viewControllers.count > 0{
            
            viewController.hidesBottomBarWhenPushed = true
        }
        //print(viewControllers.count)
        super.pushViewController(viewController, animated: animated)
        //表示压栈成功
        //print(viewControllers.count)
        
        //判断是不是最外层控制器
        if viewControllers.count > 1 {
            
            //定义一个空的可变字符串
            var title: String = ""
            //判断是否是第二层控制器
            if viewControllers.count == 2{
                
                //如果是第二层控制器就返回第一层导航栏左侧的文字
                title = viewControllers.first!.title!
                
            }else{
                
                title = "返回"
            }
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName:"navigationbar_back_withtext", target: self, action: #selector(back))
            
            viewController.navigationItem.title = "第\(viewControllers.count)层控制器"
            
        }
            }
    
    //导航栏返回事件
    @objc private func back(){
        
        self.popViewController(animated: true)
    }
    
    //MARK: -------开启pop返回手势-------
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        //判断根视图控制器不需要进行pop手势
        if viewControllers.count == 1{
            
            return false
        }
        return true
    }

}
